extends Chara

var utils = globalData.infoDs["g_AneHekUtils"]
var ctrl = globalData.infoDs["g_AneHek"]
var base = globalData.infoDs["g_AneHekBase"]
var globalBonus = globalData.infoDs["g_AneHekGB"]
var canevo:int = 1#能否进化
var file = File.new()
var charaInfo
var pic = null
var llv:int = 0
var list = ["jt","mfq","sp","txz","ypz","sf"]
#初始化
func _init():
	._init()
func _extInit():
	._extInit()
	lvupnow(self)
	#if not self.is_connected("onDelItem",self,"onDelItem"):
	#	self.connect("onDelItem",self,"onDelItem")
	pass
#改点击后弹出的英雄数据显示
func chaInfo(cha):
	yield(cha.reTimer(0.05),"timeout")
	charaInfo = sys.get_node("/root/topUi/charaInfoMsg")#获取英雄数据窗口node
	var txt = charaInfo.get_node("txt")#获取窗口上的文字
	#print(txt.bbcode_text)
	var split = txt.bbcode_text.rsplit("\n")#分行
	var res = ""
	res += split[0] + "\n[color=#FFA500]击杀[/color]：{0}\t\t[color=#FFA500]系列[/color]：{1}\n".format({"0":cha.killNum,"1":cha.camp})
	res += "╔═╗[color=#71b569]攻击范围：{2}[/color]\n║人║[color=#71b569]生命值：{0}/[color=red]{1}[/color]\t\t".format({"0":int(cha.att.hp),"1":int(cha.att.maxHp),"2":int(cha.att.atkRan)})
	res += "护盾值：[color=#00bfff]{3}[/color][/color]\n".format({"3":int(cha.shield)})
	res += "║物║[color=#ff8c00]物强：{0}[/color]\t\t\t".format({"0":int(cha.att.atk)})
	res += "[color=#00bfff]攻速：{0}%\t\t冷却：{1}%[/color]\n".format({"0":int((cha.att.spd-1)*100),"1":int(cha.att.cd*100)})
	res += "║属║[color=#00ff7f]闪避：{0}%\t\t承疗：{rehp}%[/color]\t\t\t".format({"0":int(cha.att.dod*100), "rehp":int(cha.att.reHp*100)})
	res += "[color=#ffd700]暴击：{0}%\t\t爆伤：{1}%[/color]\n".format({"0":int(cha.att.cri * 100),"1":int(cha.att.criR * 100 + 200)})
	var d1 = int(cha.att.def / (100 + cha.att.def) * 100)
	var d2 = int(cha.att.mgiDef / (100 + cha.att.mgiDef) * 100)
	res += "║性║[color=#ff4500]物理|法术吸血：{1}%|{2}%\t\t物理|法术穿透：{3}|{4}\t\t无视护甲|魔抗：{5}%|{6}%[/color]\n".format({"1":cha.att.suck*100, "2":cha.att.mgiSuck*100, "3":cha.att.pen, "4":cha.att.mgiPen, "5":cha.att.penL*100,"6":cha.att.mgiPenL*100})
	res += "╚═╝[color=#48d1cc]物理防御：{0}[{1}%][/color]\t\t\t[color=#6a5acd]魔法防御：{2}[{3}%][/color]".format({"0":int(cha.att.def),"1":d1,"2":int(cha.att.mgiDef),"3":d2})
	res += "\t\t\t[color=#ffa500]最终增伤：[{0}%]\t\t最终减伤：[{1}%][/color]\n".format({"0":cha.att.atkR*100,"1":cha.att.defR*100})
	var ik = 6
	split[6] = split[6].right(8)
	while ik < split.size() - 1:
		split[ik] = split[ik].replace("[color=#7a70fe]","").replace("[color=#4080c6]","").replace("[color=#ba7aff]","")
		if split[ik].find("[cd") != -1:
			res += "[color=#ffa500]" + split[ik] + "\n"
		else:
			res += "[color=#71b569]" + split[ik] + "\n"
		ik += 1
	#for i in cha.skillStrs:
	#	if i[0] == "[":
	#		res += "[color=#00ff00]" + i + "[/color]\n"
	#	else:
	#		res += "[color=yellow]" + i + "[/color]\n"
	#print(res)
	txt.bbcode_text = res
	txt.margin_top = 20
	var btnpos = Vector2(850,603)

	if cha.team == 1:
		if cha.id.find("cTDZ") != -1 && cha.id.count("_") == 3:
			charaInfo.get_node("Button").hide()
			var btns = utils.cBtns(charaInfo, self, "进化！ ", "Evo", [cha, charaInfo], btnpos - Vector2(185,0)) 
			btns.set_custom_minimum_size(Vector2(300,50))
			if !cha.sureEvo:btns.disabled = true
		if cha.id == "cTDZ":#如果是召唤石
			btnpos.x += 120
			utils.cBtns(charaInfo, self, " 召唤 ", "rndEvo", [cha, charaInfo], btnpos) 
			charaInfo.get_node("Button").text = "定向召唤"
			btnpos.x += 75
		elif cha.id.find("Hide") == -1:
			btnpos.x += 120
			utils.cBtns(charaInfo, self, " 升级 ", "lvups", [cha, charaInfo], btnpos) 
			btnpos.x += 75
			for i in range(6):
				var itemBt = load("res://ui/item/item.tscn").instance()
				var tit = sys.newItem("i_Hide_rw_"+list[i])
				itemBt.init(tit)
				itemBt.isDrag = false
				charaInfo.add_child(itemBt)
				itemBt.set_position(Vector2(1400,20 + i * 50))
				var val = 0
				val = utils[list[i]]
				var lb = utils.cLb(str(val),charaInfo)
				lb.set_position(Vector2(1450,25 + i * 50))
		else:btnpos.x += 120
		utils.cBtns(charaInfo, self, "一键卸装", "removeAllEquip", [cha], btnpos)
		#print(cha.id)
		if cha.id.find("cex___Hide_") != -1 && utils.st.find(cha.id) == -1:
		#	print(1)
			if !skill1unlock:
				utils.cBtns(charaInfo, self,"解锁技能1","unlockskill", [1,charaInfo],Vector2(1320,400))
			utils.cBtns(charaInfo, self,"升级技能1","upskill", [1,charaInfo],Vector2(1450,400))
			if !skill2unlock:
				utils.cBtns(charaInfo, self,"解锁技能2","unlockskill", [2,charaInfo],Vector2(1320,450))
			utils.cBtns(charaInfo, self,"升级技能2","upskill", [2,charaInfo],Vector2(1450,450))
			if !skill3unlock:
				utils.cBtns(charaInfo, self,"解锁技能3","unlockskill", [3,charaInfo],Vector2(1320,500))
			utils.cBtns(charaInfo, self,"升级技能3","upskill", [3,charaInfo],Vector2(1450,500))
#解锁英雄技能
func unlockskill(id,win):
	if utils.spoint >= 2:
		utils.spoint -= 2
	else:
		sys.newBaseMsg("提示","技能点不足2点")
		return
	utils.refesh3()
	skillunlock(id)
	win.hide()
	sys.newBaseMsg("提示","技能解锁成功，技能点-2")
#升级技能
func upskill(id,win):
	match id:
		1:
			if skill1lv == 5:
				sys.newBaseMsg("提示","技能已满级")
				return
		2:
			if skill2lv == 5:
				sys.newBaseMsg("提示","技能已满级")
				return
		3:
			if skill3lv == 5:
				sys.newBaseMsg("提示","技能已满级")
				return
	if utils.spoint >= 1:
		utils.spoint -= 1
	else:
		sys.newBaseMsg("提示","技能点不足1点")
		return
	utils.refesh3()
	match id:
		1:
			skill1lv += 1
		2:
			skill2lv += 1
		3:
			skill3lv += 1
	skillunlock(id)
	win.hide()
	sys.newBaseMsg("提示","技能升级成功，技能点-1")
#野怪系进化
func Evo(cha,win):
	var waitdel = []
	waitdel.append(cha)
	win.hide()
	for i in sys.main.btGrid.get_children():
		if waitdel.size() == 3:break
		if  i.get_child(0).id == cha.id:
			waitdel.append(i.get_child(0))
	if waitdel.size() < 3:
		var chas = cha.getAllChas(2)
		for i in chas:
			if waitdel.size() == 3:break
			if i.id  == cha.id && i != cha:
				waitdel.append(i)
	if waitdel.size() == 3:
		var ids = cha.id.replace("_","").replace("cTDZ","cex___TDZ")
		print(ids)
		var ncha = sys.main.evoChara(cha,ids)
		ncha.isDrag = true
	else:
		sys.newBaseMsg("提示","角色不足，无法进化")
#随机召唤
func rndEvo(cha,win):
	win.hide()
	cha.delself()
	var ids = sys.rndRan(0,100000) % 4 + 1
	var ncha = sys.main.newChara("cTDZ_" + str(ids))
	sys.main.player.addCha(ncha)
#删除英雄
func delself():
	removeAllEquip(self)
	var node = self.get_parent()
	self.remove()
	if self.isItem:
		node.queue_free()
#一键卸装
func removeAllEquip(cha):
	var pos = cha.items.size()-1
	while pos >=0:
		cha.delItem(cha.items[pos])
		pos -=1
#承接回游戏的战斗结束
func _onBattleEnd():
	._onBattleEnd()
#承接回游戏的onhurt
func _onHurt(atkInfo):
	._onHurt(atkInfo)
#修正承疗
func _upS():
	if self.att.reHp < -1:
		self.att.reHp = -1
#英雄技能相关
var skill1unlock = false
var skill2unlock = false
var skill3unlock = false
var skill1lv = 0
var skill2lv = 0
var skill3lv = 0
func skillunlock(id):
	#print(id," ",skill1lv)
	#print(skillStrs[id])
	match id:
		1:
			if skillStrs[2].find("未解锁") != -1:
				skillStrs[2] = skillStrs[2].replace("[color=#666666][未解锁][/color]","[已解锁]") + "[Lv"+str(skill1lv) + "]"
			else:
				skillStrs[2] = skillStrs[2].replace("[Lv" + str(skill1lv - 1) + "]","[Lv"+str(skill1lv) + "]")
			skill1unlock = true
				
		2:
			if skillStrs[3].find("未解锁") != -1:
				skillStrs[3] = skillStrs[3].replace("[color=#666666][未解锁][/color]","[已解锁]") + "[Lv"+str(skill2lv) + "]"
			else:
				skillStrs[3] = skillStrs[3].replace("[Lv" + str(skill2lv - 1) + "]","[Lv"+str(skill2lv) + "]")
			skill2unlock = true
		3:
			if skillStrs[4].find("未解锁") != -1:
				skillStrs[4] = skillStrs[4].replace("[color=#666666][未解锁][/color]","[已解锁]") + "[Lv"+str(skill3lv) + "]"
			else:
				skillStrs[4] = skillStrs[4].replace("[Lv" + str(skill3lv - 1) + "]","[Lv"+str(skill3lv) + "]")
			skill3unlock = true

#————————————————————————————————————————————————————升级———————————————————————————————————————————————————————————#
func lvups(cha,win):
	var dit = {"刺客":utils.txz,"战士":utils.sp,"牧师":utils.ypz,"法师":utils.mfq,"射手":utils.jt,"软泥怪":utils.sp,"骷髅":utils.sp,"娜迦":utils.mfq}
	win.queue_free()
	print(dit[cha.camp])
	if cha.id.find("Hide") != -1:return
	var flag = null
	var flag2 = null
	if utils.sf >= 1:
		flag = 1
	if dit[cha.camp] >= 1:
		flag2 = 1
		
	if flag && flag2: 
		if cha.llv == 5:
			sys.newBaseMsg("提示","角色已满级")
			return
		utils.sf -= 1
		match cha.camp:
			"刺客":utils.txz -=1
			"战士":utils.sp -= 1
			"牧师":utils.ypz -= 1
			"法师":utils.mfq -= 1
			"射手":utils.jt -=1
			"软泥怪":utils.sp -=1
			"骷髅":utils.sp -=1
			"娜迦":utils.mfq -=1
		yield(reTimer(0.01),"timeout")
		#self.delItem(flag)
		#self.delItem(flag2)
		#sys.main.player.delItem(flag)
		#sys.main.player.delItem(flag2)
		cha.llv += 1
		lvupnow(cha)
		sys.newBaseMsg("提示","升级成功")
	else:
		var txt = "不足"
		if !flag:
			txt = "神发" + txt
		if !flag2:
			txt = "阵营指定材料" + txt
		sys.newBaseMsg("提示",txt)
func lvupnow(cha):
	#print(llv)
	yield(reTimer(0.02),"timeout")
	if !cha.llv:return
	if cha.chaName.find("·Lv") != -1:	
		cha.chaName = cha.chaName.left(cha.chaName.find("·Lv"))
	cha.chaName = cha.chaName + "·Lv" + str(llv)